﻿Imports System.Windows.Forms
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst.swUserPreferenceStringValue_e


Public Class frmoption




    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim Ini As New Ini
        Dim Csv_Attribs As String
        Dim Templates_DrwDot As String
        Dim Templates_Slddrt As String

        Part_Number = ComboBox1.SelectedItem
        SwVersion = ComboBox2.SelectedItem
        CAD_To = CheckBox1.CheckState
        CAD_Add_Part_NO = CheckBox2.CheckState
        Compear = CheckBox3.CheckState
        AutoUpdate = CheckBox4.CheckState

        Csv_Attribs = RichTextBox1.Text.ToString
        Templates_DrwDot = TextBox2.Text.ToString
        Templates_Slddrt = TextBox3.Text.ToString
        Custom_Properties_File = TextBox4.Text.ToString

        Ini.INIWrite(Ini_File, "SwTool", "SwVersion", SwVersion)
        Ini.INIWrite(Ini_File, "SwTool", "Part_Number", Part_Number)
        Ini.INIWrite(Ini_File, "SwTool", "CAD_To", CAD_To)
        Ini.INIWrite(Ini_File, "SwTool", "CAD_Add_Part_NO", CAD_Add_Part_NO)
        Ini.INIWrite(Ini_File, "SwTool", "Compear", Compear)
        Ini.INIWrite(Ini_File, "SwTool", "Csv_Attribs", Csv_Attribs)
        Ini.INIWrite(Ini_File, "SwTool", "Templates_DrwDot", Templates_DrwDot)
        Ini.INIWrite(Ini_File, "SwTool", "Templates_SldDrt", Templates_Slddrt)
        Ini.INIWrite(Ini_File, "SwTool", "Custom_Properties_File", Custom_Properties_File)
        Ini.INIWrite(Ini_File, "SwTool", "AutoUpdate", AutoUpdate)

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Dispose()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub

    Private Sub frmoption_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If IsTopMost = True Then
            frmmain.TopMost = IsTopMost
        End If
        Me.Close()
    End Sub

    Private Sub frmoption_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Private Sub frmoption_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        On Error Resume Next
        Dim i As Integer
        Dim Ini As New Ini

        ComboBox1.Items.Clear()
        For i = 0 To UBound(CustomPropertyNames)
            ComboBox1.Items.Add(CustomPropertyNames(i))
            ComboBox3.Items.Add(CustomPropertyNames(i))
        Next

        ComboBox1.SelectedItem = Part_Number
        ComboBox2.SelectedItem = SwVersion.ToString
        CheckBox1.CheckState = CAD_To
        CheckBox2.CheckState = CAD_Add_Part_NO
        CheckBox3.CheckState = Compear
        CheckBox4.CheckState = AutoUpdate

        RichTextBox1.Text = Ini.INIRead(Ini_File, "SwTool", "Csv_Attribs")
        TextBox2.Text = Ini.INIRead(Ini_File, "SwTool", "Templates_DrwDot")
        TextBox3.Text = Ini.INIRead(Ini_File, "SwTool", "Templates_SldDrt")
        TextBox4.Text = Custom_Properties_File

        Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Bottom - Me.Height) / 2)

    End Sub

    '添加bom标项目
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error Resume Next
        Dim Csv_Row As String
        If RichTextBox1.Text.ToString = "" Then
            Csv_Row = ComboBox3.SelectedItem.ToString
        Else
            Csv_Row = "," & ComboBox3.SelectedItem.ToString
        End If
        RichTextBox1.AppendText(Csv_Row)

    End Sub

    '选择工程图模板文件
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim swApp As SldWorks

        swApp = CreateObject("SldWorks.Application")

        With OpenFileDialog1
            .Reset()
            .Multiselect = False
            .InitialDirectory = GetParentFolder(swApp.GetUserPreferenceStringValue(swDefaultTemplateDrawing))
            .Title = "添加 DrwDot 工程图模板文件"
            .Filter = "SW工程图模板(*.drwdot)|*.drwdot"
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                TextBox2.Text = .FileName
            Else
                Exit Sub
            End If
        End With
    End Sub

    '选择图纸格式文件
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim swApp As SldWorks

        swApp = CreateObject("SldWorks.Application")

        With OpenFileDialog1
            .Reset()
            .Multiselect = False
            .InitialDirectory = swApp.GetUserPreferenceStringValue(swFileLocationsSheetFormat)
            .Title = "添加 Slddrt 图纸格式文件"
            .Filter = "SW工程图模板(*.Slddrt)|*.slddrt"
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                TextBox3.Text = .FileName
            Else
                Exit Sub
            End If
        End With
    End Sub


    '选择自定义属性文件
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim swApp As SldWorks

        swApp = CreateObject("SldWorks.Application")

        With OpenFileDialog1
            .Reset()
            .Multiselect = False
            .InitialDirectory = swApp.GetUserPreferenceStringValue(swFileLocationsCustomPropertyFile)
            .Title = "自定义属性文件"
            .Filter = "properties.txt|properties.txt"
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                Custom_Properties_File = .FileName
                TextBox4.Text = Custom_Properties_File
            Else
                Exit Sub
            End If
        End With

    End Sub

    '添加为表达式
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        On Error Resume Next
        Dim Csv_Row As String
        Dim i As Short
        i = RichTextBox1.SelectionStart
        Csv_Row = "#" & ComboBox3.SelectedItem.ToString & "#"
        RichTextBox1.Text = RichTextBox1.Text.Insert(i, Csv_Row)
        RichTextBox1.Focus()
    End Sub
      
End Class
